package cn.jy.zcy.controller;

import cn.jy.zcy.bean.Msg;
import cn.jy.zcy.pojo.HistroryRJ;
import cn.jy.zcy.pojo.RuanJian;
import cn.jy.zcy.service.HistroryRJService;
import cn.jy.zcy.service.RuanJianService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class RuanJianController {
    @Autowired
    private RuanJianService ruanJianService;
    @Autowired
    private HistroryRJService histroryRJService;

    /**
     * 查询软件信息列表
     *
     * @return
     */

    //    查询所有软件信息    审核表
    @ResponseBody
    @GetMapping("/list")
    public Msg ruanJianList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pn) {
        if (pn < 1) {
            pn = 1;
        }
        PageHelper.startPage(pn, 7);
        List<RuanJian> ruanJians = ruanJianService.queryRJ();
        PageInfo<RuanJian> pageInfo = new PageInfo<RuanJian>(ruanJians, 7);
        //将分页信息传入自定义类的returnData
        return Msg.success().add("pageInfo", pageInfo);
    }

    //    查询所有软件信息   开发者表
    @ResponseBody
    @GetMapping("/history")
    public Msg ruanJianList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pn, HttpServletRequest request) {
        if (pn < 1) {
            pn = 1;
        }
        HttpSession session = request.getSession();
        Integer id = (Integer) session.getAttribute("kaifazId");
        PageHelper.startPage(pn, 7);
        List<RuanJian> ruanJians = ruanJianService.queryRJByKaid(id);
        PageInfo<RuanJian> pageInfo = new PageInfo<RuanJian>(ruanJians, 7);
        //将分页信息传入自定义类的returnData
        return Msg.success().add("pageInfo", pageInfo);
    }

    //根据 ID 查询软件信息
    @ResponseBody
    @GetMapping("/rJ/{id}")
    public Msg getRuanJian(@PathVariable("id") Integer id) {
        System.out.println("id=" + id);
        RuanJian ruanJian = ruanJianService.queryRJxx(id);
        return Msg.success().add("ruanJian", ruanJian);
    }


    //根据 ID 查询软件历史版本信息
    @ResponseBody
    @GetMapping("/history/{appid}")
    public Msg getHisRuanJian(@PathVariable("appid") Integer appid) {

        List<HistroryRJ> histroryRJS = histroryRJService.selectHistrory(appid);

        return Msg.success().add("histroryRJS", histroryRJS);
    }

    //根据 ID 修改软件状态   通过
    @ResponseBody
    @GetMapping("/rj/{id}")
    public Msg getRJ(@PathVariable("id") Integer id) {
        System.out.println("id=" + id);
        int tai = 2;
        int i = ruanJianService.updateZhuantai(id, tai);
        System.out.println("i=" + i);
        if (i > 0) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    //根据 ID 修改软件状态   上架
    @ResponseBody
    @GetMapping("/sj/{id}")
    public Msg ShanJia(@PathVariable("id") Integer id) {
        System.out.println("id=" + id);
        int tai = 4;
        int i = ruanJianService.updateZhuantai(id, tai);
        System.out.println("i=" + i);
        if (i > 0) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    //根据 ID 修改软件状态  不通过
    @ResponseBody
    @GetMapping("/rj/{id}")
    public Msg getRJs(@PathVariable("id") Integer id) {
        System.out.println("id=" + id);
        int tai = 3;
        int i = ruanJianService.updateZhuantai(id, tai);
        System.out.println("i=" + i);
        if (i > 0) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    //根据 ID 修改软件状态  下架
    @ResponseBody
    @GetMapping("/xiajia/{id}")
    public Msg XiaJia(@PathVariable("id") Integer id) {
        System.out.println("id=" + id);
        int tai = 5;
        int i = ruanJianService.updateZhuantai(id, tai);
        System.out.println("i=" + i);
        if (i > 0) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    //删除图书
    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public Msg deleteBook(@PathVariable("id") Integer id) {
        int appid = id;

        int n = ruanJianService.delHisRuanJian(appid);
        int i = ruanJianService.delRuanJian(id);
        if (n > 0) {
            if (i > 0) {
                return Msg.success().add("msg", "success");
            }
        }
        return Msg.fail().add("msg", "error");
    }


}
